

const {ccclass, property,menu} = cc._decorator;

@ccclass
@menu('Framework/EventMgr')
export default class EventMgr {

    private static _instance:EventMgr

    private static node:cc.Node




    static get instance():EventMgr{
        if(!EventMgr.node){
            EventMgr.node = new cc.Node('EventNode')
            cc.game.addPersistRootNode(EventMgr.node)
            EventMgr._instance = new EventMgr()
        }
        return this._instance
    }

    targetOff(target: any): void{
        EventMgr.node.targetOff(target)
    }

    /** 事件监听 */
    on(event: string, cb: Function, target?: object) {
        EventMgr.node.on(event, function (event) {
            cb();
        }, target);
    }

    /** 事件监听(一次) */
    once(event: string, cb: Function, target?: object) {
        EventMgr.node.once(event, function (event) {
            cb();
        }, target);
    }

    /** 事件发射 */
    emit(event: string, data?: any) {
        EventMgr.node.emit(event, data);
    }

    /** 取消事件监听 */
    off(type: string, callback: Function, target?: any) {
        EventMgr.node.off(type, callback,target);
    }
}